package com.crane.oss.common.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.crane.common.core.utils.StringUtil;
import com.crane.oss.common.bean.request.QueryDistrictRequest;
import com.crane.oss.common.dao.DistrictDao;
import com.crane.oss.common.model.District;
import com.crane.oss.common.service.DistrictService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

;

@Service("districtService")
public class DistrictServiceImpl implements DistrictService {

    private static final Logger logger = LoggerFactory.getLogger(DistrictServiceImpl.class);

    @Resource
    protected DistrictDao districtDao;

    @SuppressWarnings("deprecation")
	@Override
    @Cacheable(value = "district", key = "#root.targetClass+':parentCode:'+#queryRequest.getParentCode()", unless = " #result.size() != 0 ")
    public List<Map<String, Object>> queryList(QueryDistrictRequest queryRequest) {
        logger.info("DistrictServiceImpl queryList queryRequest={}", JSON.toJSON(queryRequest));
        List<District> list = districtDao.selectList(new LambdaQueryWrapper<District>().eq(ObjectUtils.isNotNull(queryRequest.getParentCode()),District::getParentCode,queryRequest.getParentCode()));

        List<Map<String, Object>> resultList = new ArrayList<>();
        for (District district : list) {
            Map<String, Object> resmap = new HashMap<>();
            resmap.put("code", district.getGbCode());
            resmap.put("name", district.getName());
            resultList.add(resmap);
        }
        return resultList;
    }

    @SuppressWarnings("deprecation")
	@Override
    @Cacheable(value = "district", key = "#root.targetClass+':gbCode:'+#queryRequest.getGbCode()", unless = " #result != null ")
    public String queryName(QueryDistrictRequest queryRequest) {
        logger.info("DistrictServiceImpl queryList queryName={}", JSON.toJSON(queryRequest));
        String result = "";
        Map<String, Object> map = new HashMap<>();
        if (StringUtil.isEmpty(queryRequest.getGbCode())) {
            return result;
        }
        List<District> list = districtDao.selectList(new LambdaQueryWrapper<District>().eq(ObjectUtils.isNotNull(queryRequest.getGbCode()),District::getGbCode,queryRequest.getGbCode()));
        if (list.size() != 0) {
            result = list.get(0).getName();
        }
        return result;
    }

    @SuppressWarnings("deprecation")
	@Override
    @Cacheable(value = "district", key = "#root.targetClass+':gbCode:'+#gbCode", unless = " #result != null ")
    public String getNameByCode(String gbCode) {
        logger.info("DistrictServiceImpl getNameByCode Code={}", gbCode);
        String result = null;
        if (StringUtil.isEmpty(gbCode)) {
            return result;
        }
        District district = districtDao.selectOne(new LambdaQueryWrapper<District>().eq(District::getGbCode,gbCode));
        if (null != district) {
            return district.getName();
        }
        return "";
    }

}
